// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2025-02-25

package consul

import (
	"fmt"
	"gitee.com/go-libs/console"
	"kv/providers"
	"regexp"
)

const (
	defaultHost = "127.0.0.1:8500"

	optionHost     = "host"
	optionName     = "name"
	optionOverride = "override"
	optionPath     = "path"

	providerDesc = "Read configuration from consul server then save as local files"
	providerName = "consul"
)

var (
	errNameNotSpecified      = fmt.Errorf(`name of kv not specified`)
	errPathNotSpecified      = fmt.Errorf(`location path not specified`)
	errPathNotValidDirectory = fmt.Errorf(`location path not a valid directory`)

	options = map[string]providers.Option{
		optionHost: {
			DefaultValue: defaultHost,
			Description:  "Consul server address",
			Kind:         console.KindString,
			Short:        'h',
		},
		optionName: {
			Description: "Name of KV, eg: go-apps/gmq",
			Kind:        console.KindString,
			Required:    true,
			Short:       'n',
		},
		optionPath: {
			Description: "Location for Generated files, eg: /data/config",
			Kind:        console.KindString,
			Required:    true,
			Short:       'p',
		},
		optionOverride: {
			Description: "Override the generated file if exists",
			Kind:        console.KindNull,
			Short:       'o',
		},
	}

	regexEndWithSlash           = regexp.MustCompile(`/+$`)
	regexMatchFilename          = regexp.MustCompile(`^(.+)/([^/]+)$`)
	regexStartWithProtocol      = regexp.MustCompile(`^(?i)https?://`)
	regexWithCompatibleVariable = regexp.MustCompile(`(?i)kv://([,./_a-z0-9-]+)`)
	regexWithVariable           = regexp.MustCompile(`{{([^}]+)}}`)

	regexYamlFile     = regexp.MustCompile(`\.ya?ml$`)
	regexYamlMultiple = regexp.MustCompile(`\n---([^\n\---])`)
)
